package org.item.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.item.common.bean.SysMenu;
import org.item.common.service.IMenuService;
import org.item.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @PostMapping("/")
    public ResultData createMenu(@RequestBody SysMenu menu) {
        SysMenu data = menuService.insert(menu);
        return ResultData.success("菜单创建成功", data);
    }

    @PutMapping("/")
    public ResultData updateMenu(@RequestBody SysMenu menu) {
        SysMenu data = menuService.update(menu);
        return ResultData.success("菜单更新成功", data);
    }

    @DeleteMapping("/{id}")
    public ResultData deleteMenu(@PathVariable Integer id) {
        int result = menuService.delete(id);
        if (result > 0) {
            return ResultData.success();
        }
        return ResultData.fail();
    }

    @GetMapping("/{id}")
    public ResultData getMenuById(@PathVariable Integer id) {
        SysMenu menu = menuService.getById(id);
        return ResultData.success(menu);
    }

    @GetMapping("/list")
    public ResultData getMenuList(SysMenu menu) {
        return ResultData.success(menuService.getList(menu));
    }

    @GetMapping("/page")
    public ResultData getMenuByPage(IPage page, @RequestBody SysMenu menu) {
        IPage result = menuService.getListPage(page, menu);
        return ResultData.success(result);
    }

    @PutMapping("/visible/{id}")
    public ResultData toggleMenuVisibility(@PathVariable Integer id) {
        int result = menuService.setVisble(id);
        if (result > 0) {
            return ResultData.success();
        }
        return ResultData.fail();
    }
}